#include "TimeWidget.h"
#include "Timebar.h"
#include "TreeItemGroup.h"

#include <QPainter>
#include <QVBoxLayout>
#include <QSpacerItem>

TimeWidget::TimeWidget(QWidget *parent, Qt::WindowFlags f)
    : QWidget(parent, f)
    , _dialPos(-1, 0)
{
    QVBoxLayout *pLayout = new QVBoxLayout(this);
    pLayout->setContentsMargins(0, 0, 0, 0);
    pLayout->setSpacing(3);

    TimeBar* pBar = new TimeBar();
    pLayout->addWidget(pBar);

    TreeItemGroup* pTree = new TreeItemGroup();
    pLayout->addWidget(pTree);

    QSpacerItem* pSpaceItem = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    pLayout->addSpacerItem(pSpaceItem);

    QObject::connect(pBar, SIGNAL(signalRepaintDialPos(QPoint)), this, SLOT(slotRepaintDialPos(QPoint)));
    slotRepaintDialPos(pBar->getCurDialPos());

    this->setStyleSheet("background-color:lightgray");
}

TimeWidget::~TimeWidget()
{

}

void TimeWidget::slotRepaintDialPos(QPoint pos)
{
    _dialPos = pos;
    repaint();
}

void TimeWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);    // 反走样

    if (_dialPos.x() > 0 && _dialPos.x() < this->width())
    {
        painter.setPen(QPen(QColor("red"), 1.8));    // 设置画笔颜色、宽度
        painter.drawLine(_dialPos, QPoint(_dialPos.x(), this->height()));
    }
}
